/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifips5.nal.epizza.web;

import com.ifips5.nal.epizza.util.JDBCManager;
import com.ifips5.nal.epizza.web.model.Boisson;
import com.ifips5.nal.epizza.web.model.Menu;
import com.ifips5.nal.epizza.web.model.Pizza;
import com.ifips5.nal.epizza.web.model.TypeMenu;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.sql.rowset.WebRowSet;

/**
 *
 * @author Moi
 */
public class MenuManager
{

    private BeanManager beanManager = null;
    private List<TypeMenu> menuList = new LinkedList<TypeMenu>();
    private Map<Integer, TypeMenu> menuMap = new HashMap<Integer, TypeMenu>();
    private TypeMenu currentMenu = null;
    
    private List<Menu> userMenuList = new LinkedList<Menu>();
    private Map<Integer, Menu> userMenuMap = new HashMap<Integer, Menu>();
    private Menu currentUserMenu = null;

    /** Creates a new instance of BoissonManager */
    public MenuManager()
    {
    }

    public MenuManager(BeanManager beanManager)
    {
        this.beanManager = beanManager;
    }

    public void loadTypeMenu(String id)
    {
        String command = "SELECT * FROM typemenu WHERE idtypemenu = " + id;
        int[] keyCol = {1};
        WebRowSet wrs = JDBCManager.getInstance().getRowSet(command, keyCol);
        try
        {
            if (wrs.next())
            {
                this.currentMenu = new TypeMenu(wrs.getInt(1), wrs.getString(2), wrs.getString(3), wrs.getDouble(4), wrs.getInt(5), wrs.getInt(6));
            }
            wrs.close();
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    public void loadTypeMenu()
    {
        String command = "SELECT * FROM typemenu";
        int[] keyCol = {1};

        WebRowSet wrs = JDBCManager.getInstance().getRowSet(command, keyCol);
        try
        {
            while (wrs.next())
            {
                TypeMenu currentTypeMenu = new TypeMenu(wrs.getInt(1), wrs.getString(2), wrs.getString(3), wrs.getDouble(4), wrs.getInt(5), wrs.getInt(6));

                
                this.menuMap.put(wrs.getInt(1), currentTypeMenu);
                this.menuList.add(currentTypeMenu);
            }
            wrs.close();
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
      
    }
    
    
    public Menu getUserMenu(int id)
    {
        Menu currentUserMenu = new Menu();
//        String command =    "SELECT * FROM menu, menu_has_pizza, menu_has_boisson " +
//                            "WHERE menu.idMenu = "+id +" "+
//                            "AND menu.idMenu = menu_has_boisson.Menu_idMenu " +
//                            "AND menu.idMenu = menu_has_pizza.Menu_idMenu";

        
        String command =    "SELECT * FROM menu, menu_has_pizza " +
                    "WHERE menu.idMenu = "+id +" "+
                    "AND menu.idMenu = menu_has_pizza.Menu_idMenu";

        
        int[] keyCol = {1};

        WebRowSet wrs = JDBCManager.getInstance().getRowSet(command, keyCol);
        try
        {   
        
            while (wrs.next())
            {
                currentUserMenu.getPizzaList().add(beanManager.getPizzaManager().getPizzaMap().get(wrs.getInt("Pizza_idPizza")));
                //currentUserMenu.getBoissonList().add(beanManager.getBoissonManager().getBoissonMap().get(wrs.getInt("Boisson_idBoisson")));
                currentUserMenu.setTypeMenu(beanManager.getMenuManager().getMenuMap().get(wrs.getInt(2)));
            }
            
            
         command =    "SELECT * FROM menu, menu_has_boisson " +
                    "WHERE menu.idMenu = "+id +" "+
                    "AND menu.idMenu = menu_has_boisson.Menu_idMenu ";

         wrs = JDBCManager.getInstance().getRowSet(command, keyCol);
         while (wrs.next())
            {
               // currentUserMenu.getPizzaList().add(beanManager.getPizzaManager().getPizzaMap().get(wrs.getInt("Pizza_idPizza")));
                currentUserMenu.getBoissonList().add(beanManager.getBoissonManager().getBoissonMap().get(wrs.getInt("Boisson_idBoisson")));
            }
         
        wrs.close();
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        return currentUserMenu;
    }
    
    
    
    public void insertUserMenu(Menu menu, int idCommande)
    {
        int idInsertMenu = -1;
        
        try
        {
            Connection connection = JDBCManager.getInstance().getConnection();
            Statement stmt = connection.createStatement();
            String command = "INSERT INTO menu(TypeMenu_idTypeMenu) VALUES ( "+menu.getTypeMenu().getIdTypeMenu()+")";
        
            stmt.executeUpdate(command, stmt.RETURN_GENERATED_KEYS);
            ResultSet cle = stmt.getGeneratedKeys();
            if(cle.next())
            {
                idInsertMenu = cle.getInt(1);
            }
            
            // Insertion des pizzas
            stmt = connection.createStatement();
            for (Pizza currentPizza : menu.getPizzaList())
            {
                stmt.addBatch("INSERT INTO menu_has_pizza VALUES( "+idInsertMenu+", "+currentPizza.getIdPizza()+")");
            }
            
            // Insertion des boissons
            for (Boisson currentBoisson : menu.getBoissonList())
            {
                stmt.addBatch("INSERT INTO menu_has_boisson VALUES( "+idInsertMenu+", "+currentBoisson.getIdBoisson()+")");
            }
            
            stmt.addBatch("INSERT INTO menu_has_commande VALUES( "+idInsertMenu+", "+idCommande+")");
            
            stmt.executeBatch();   
            connection.commit();
            connection.close();    
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }        
    }
    
    public void searchMenu(String pattern)
    {
        String[] tab = pattern.split("\\s");
        String command = "SELECT * FROM typemenu WHERE name LIKE '%"+tab[0]+"%' ";
        
        for (int i = 1; i < tab.length; i++) 
        {
            String currentPattern = tab[i];
            command += "OR name LIKE '%"+currentPattern+"%' ";
        }
        
        int[] keyCol = {1};
        WebRowSet wrs =  JDBCManager.getInstance().getRowSet(command, keyCol);
        try
        {
            while(wrs.next())
            {
                TypeMenu currentMenu =  new TypeMenu(wrs.getInt(1), wrs.getString(2), wrs.getString(3), wrs.getDouble(4), wrs.getInt(5), wrs.getInt(6));
                menuMap.put(wrs.getInt(1), currentMenu);
                menuList.add(currentMenu);
            }
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }                  
    }    
    

    public List<TypeMenu> getMenuList()
    {
        return menuList;
    }

    public void setMenuList(List<TypeMenu> menuList)
    {
        this.menuList = menuList;
    }

    public Map<Integer, TypeMenu> getMenuMap()
    {
        return menuMap;
    }

    public void setMenuMap(Map<Integer, TypeMenu> menuMap)
    {
        this.menuMap = menuMap;
    }

    public BeanManager getBeanManager()
    {
        return beanManager;
    }

    public void setBeanManager(BeanManager beanManager)
    {
        this.beanManager = beanManager;
    }

    public TypeMenu getCurrentMenu()
    {
        return currentMenu;
    }

    public void setCurrentMenu(TypeMenu currentMenu)
    {
        this.currentMenu = currentMenu;
    }

    public List<Menu> getUserMenuList()
    {
        return userMenuList;
    }

    public void setUserMenuList(List<Menu> userMenuList)
    {
        this.userMenuList = userMenuList;
    }

    public Map<Integer, Menu> getUserMenuMap()
    {
        return userMenuMap;
    }

    public void setUserMenuMap(Map<Integer, Menu> userMenuMap)
    {
        this.userMenuMap = userMenuMap;
    }

    public Menu getCurrentUserMenu()
    {
        return currentUserMenu;
    }

    public void setCurrentUserMenu(Menu currentUserMenu)
    {
        this.currentUserMenu = currentUserMenu;
    }
//
//    
//    public void deleteBoisson(String idBoisson)
//    {
//        try
//        {
//            Connection connection = JDBCManager.getInstance().getConnection();
//            Statement stmt = connection.createStatement();
//            stmt.addBatch("DELETE FROM boisson WHERE idBoisson=\""+idBoisson+"\"");
//            //stmt.addBatch("DELETE FROM ingredient_has_pizza WHERE Ingredient_idIngredient="+idIngredient);
//            stmt.executeBatch();   
//            connection.commit();
//            connection.close();    
//        } catch (SQLException ex)
//        {
//            ex.printStackTrace();
//        }
//    }
//    
//    
//    public void deleteBoisson(String[] delBoissons)
//    {
//        try
//        {
//            Connection connection = JDBCManager.getInstance().getConnection();
//            Statement stmt = connection.createStatement();
//            
//            for(int i = 0; i < delBoissons.length ; i++)
//            {
//                stmt.addBatch("DELETE FROM Boisson WHERE idBoisson="+delBoissons[i]);
//         //       stmt.addBatch("DELETE FROM ingredient_has_pizza WHERE Ingredient_idIngredient="+delIngredients[i]);
//            }
//
//            stmt.executeBatch();   
//            connection.commit();
//            connection.close();    
//        } catch (SQLException ex)
//        {
//            ex.printStackTrace();
//        }    
//    }
//    
//    public void insertBoisson(String name, String prix)
//    {
//        double dPrix;
//        try
//        {
//            dPrix = Double.parseDouble(prix);
//        } catch (NumberFormatException ex)
//        {
//            dPrix = 1.0;
//        }
//
//        try
//        {
//            Connection connection = JDBCManager.getInstance().getConnection();
//            Statement stmt = connection.createStatement();
//            stmt.addBatch("INSERT INTO boisson(name, prix) VALUES ( \""+name+"\", "+dPrix+")");
//            stmt.executeBatch();   
//            connection.commit();
//            connection.close();    
//        } catch (SQLException ex)
//        {
//            ex.printStackTrace();
//        }
//    }
//    
//    public void updateBoisson(String id, String name, String prix)
//    {
//        double dPrix;
//        try
//        {
//            dPrix = Double.parseDouble(prix);
//        } catch (NumberFormatException ex)
//        {
//            dPrix = 1.0;
//        }
//
//        try
//        {
//            Connection connection = JDBCManager.getInstance().getConnection();
//            Statement stmt = connection.createStatement();
//            stmt.addBatch("UPDATE boisson SET name = \""+name+"\", prix="+dPrix+" WHERE idBoisson="+id);
//            stmt.executeBatch();   
//            connection.commit();
//            connection.close();    
//        } catch (SQLException ex)
//        {
//            ex.printStackTrace();
//        }
//    }
}
